package com.x.client.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.client.model.SellCar;
import com.x.client.rpc.SellCarRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 卖车信息
 */
@RestController
@RequestMapping(value = "/token/sellCar")
public class SellCarController extends BaseController{

	@Reference
	private SellCarRpcService sellCarRpcService;// 车辆收藏服务

	/**
	 * 新增卖车信息
	 * @param sellCar
	 * @return
	 */
	@RequestMapping(value = "/insertSellCar", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel insertSellCar(@Validated SellCar sellCar, BindingResult bindingResult, HttpServletRequest request){
		if (bindingResult.hasErrors()){
			List<ObjectError> errors = bindingResult.getAllErrors();
			return new ResultModel(ResultCode.RESULT_FAIL, errors.get(0).getDefaultMessage());
		}
		String source = request.getHeader("source");
		if (StringUtils.isEmpty(source)){
			return new ResultModel(ResultCode.RESULT_FAIL, "线索来源不能为空！");
		}
		sellCar.setSellCarState(1);
		sellCar.setSellCarSource(source);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功", sellCarRpcService.insertSellCar(sellCar));
	}

	/**
	 * 查询卖车信息列表
	 * @param sellCar
	 * @return
	 */
	@RequestMapping(value = "/selectSellCarList", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel selectSellCarList(SellCar sellCar){
		if (StringUtils.isEmpty(sellCar.getUserId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", sellCarRpcService.selectSellCarList(sellCar));
	}

	/**
	 * 查询卖车信息数量
	 * @param sellCar
	 * @return
	 */
	@RequestMapping(value = "/selectSellCarListNum", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel selectSellCarListNum(SellCar sellCar){
		if (StringUtils.isEmpty(sellCar.getUserId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		Map<String, Object> res = new HashMap<>();
		res.put("total", sellCarRpcService.selectSellCarListNum(sellCar));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询全部卖车信息列表
	 * @param sellCar
	 * @return
	 */
	@RequestMapping(value = "/selectAllSellCarList", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel selectAllSellCarList(SellCar sellCar){
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", sellCarRpcService.selectSellCarList(sellCar));
	}

	/**
	 * 查询全部卖车信息数量
	 * @param sellCar
	 * @return
	 */
	@RequestMapping(value = "/selectAllSellCarListNum", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel selectAllSellCarListNum(SellCar sellCar){
		Map<String, Object> res = new HashMap<>();
		res.put("total", sellCarRpcService.selectSellCarListNum(sellCar));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 修改卖车信息
	 * @param sellCar
	 * @return
	 */
	@RequestMapping(value = "/updateSellCar", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel updateSellCar(SellCar sellCar){
		if (StringUtils.isEmpty(sellCar.getSellCarId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功", sellCarRpcService.updateSellCar(sellCar));
	}
}
